package com.wxy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxy.entity.Saleacdata;
import com.wxy.entity.Saledata;
import com.wxy.entity.Saledetaildata;
import com.wxy.entity.Salemonthdata;
import com.wxy.mapper.SaleacdataMapper;
import com.wxy.mapper.SaledataMapper;
import com.wxy.mapper.SaledetaildataMapper;
import com.wxy.mapper.SalemonthdataMapper;
import com.wxy.service.EchartAnalysisService;
import com.wxy.vo.EchartAnalysis;
import com.wxy.vo.EchartAnalysisData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EchartAnalysisServiceImpl implements EchartAnalysisService {

    @Autowired
    private SalemonthdataMapper salemonthdataMapper;

    @Autowired
    private SaledataMapper saledataMapper;

    @Autowired
    private SaledetaildataMapper saledetaildataMapper;

    @Override
    public EchartAnalysis getOwnAnalysis() {
        QueryWrapper queryWrapperFirst = new QueryWrapper();
        queryWrapperFirst.eq("weight",1);
        List<Saledetaildata> Saledetaildata1 = saledetaildataMapper.selectList(queryWrapperFirst);

        QueryWrapper queryWrapperSecond = new QueryWrapper();
        queryWrapperSecond.eq("weight",2);
        List<Saledetaildata> Saledetaildata2 = saledetaildataMapper.selectList(queryWrapperSecond);

        QueryWrapper queryWrapperThird = new QueryWrapper();
        queryWrapperThird.eq("weight",3);
        List<Saledetaildata> Saledetaildata3 = saledetaildataMapper.selectList(queryWrapperThird);

        QueryWrapper queryWrapperForuth = new QueryWrapper();
        queryWrapperForuth.eq("weight",4);
        List<Saledetaildata> Saledetaildata4 = saledetaildataMapper.selectList(queryWrapperForuth);

        EchartAnalysis echartAnalysis = new EchartAnalysis();
        //一季度数据
        List<String> firstList = new ArrayList<>();
        firstList.add("权重一");
        List<String> secondList = new ArrayList<>();
        secondList.add("权重二");
        List<String> thirdList = new ArrayList<>();
        thirdList.add("权重三");
        List<String> forthList = new ArrayList<>();
        forthList.add("权重四");
        List<List> lists = new ArrayList<>();
        List<String> product = new ArrayList<>();
        product.add("product");

        Integer listNum = Saledetaildata1.size()<=Saledetaildata2.size()?Saledetaildata2.size():Saledetaildata1.size();
        listNum = listNum<=Saledetaildata3.size()?Saledetaildata3.size():listNum;
        listNum = listNum<=Saledetaildata4.size()?Saledetaildata4.size():listNum;
        for (Integer a=1;a<=listNum;a++){
            product.add(String.valueOf(a));
        }

        //平均数
        Integer average = 0;
        Integer i = 0;
        //封装到表
        for (Saledetaildata saledetaildata: Saledetaildata1) {
            Integer profit =  saledetaildata.getDetailProjectProfit();
            firstList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledetaildata saledetaildata : Saledetaildata2) {
            Integer profit =  saledetaildata.getDetailProjectProfit();
            secondList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledetaildata saledetaildata : Saledetaildata3) {
            Integer profit =  saledetaildata.getDetailProjectProfit();
            thirdList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledetaildata saledetaildata : Saledetaildata4) {
            Integer profit =  saledetaildata.getDetailProjectProfit();
            forthList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        lists.add(product);
        lists.add(firstList);
        lists.add(secondList);
        lists.add(thirdList);
        lists.add(forthList);
        echartAnalysis.setDataset(lists);

        List<EchartAnalysisData> seriesList = new ArrayList<>();
        for (Integer j=1;j<=4;j++){
            EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
            echartAnalysisData.setType("line");
            Map<String,String> map = new HashMap<String,String>();
            map.put("focus","series");
            echartAnalysisData.setEmphasis(map);
            echartAnalysisData.setSeriesLayoutBy("row");
            echartAnalysisData.setSmooth("true");
            seriesList.add(echartAnalysisData);
        }
        EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
        echartAnalysisData.setType("pie");
        echartAnalysisData.setId("pie");
        echartAnalysisData.setRadius("30%");

        List<String> center =  new ArrayList();
        center.add("50%");
        center.add("25%");
        echartAnalysisData.setCenter(center);

        Map<String,String> map = new HashMap<String,String>();
        map.put("focus","self");
        echartAnalysisData.setEmphasis(map);

        Map<String,String> maplabel = new HashMap<String,String>();
        maplabel.put("formatter","{b}: {@2012} ({d}%)");
        echartAnalysisData.setLabel(maplabel);

        Map<String,String> encode = new HashMap<String,String>();
        encode.put("itemName","product");
        encode.put("value","1");
        echartAnalysisData.setEncode(encode);
        seriesList.add(echartAnalysisData);
        echartAnalysis.setSeries(seriesList);
        echartAnalysis.setAverage(average/i);
        return echartAnalysis;
    }

    @Override
    public EchartAnalysis getQuarterAnalysis() {
        //1,2,3,4
        QueryWrapper queryWrapperFirst = new QueryWrapper();
        queryWrapperFirst.ge("time","2022-01");
        queryWrapperFirst.le("time","2022-03");
        List<Salemonthdata> salemonthdataList1 = salemonthdataMapper.selectList(queryWrapperFirst);

        QueryWrapper queryWrapperSecond = new QueryWrapper();
        queryWrapperSecond.ge("time","2022-04");
        queryWrapperSecond.le("time","2022-06");
        List<Salemonthdata> salemonthdataList2 = salemonthdataMapper.selectList(queryWrapperSecond);

        QueryWrapper queryWrapperThird = new QueryWrapper();
        queryWrapperThird.ge("time","2022-07");
        queryWrapperThird.le("time","2022-09");
        List<Salemonthdata> salemonthdataList3 = salemonthdataMapper.selectList(queryWrapperThird);

        QueryWrapper queryWrapperForuth = new QueryWrapper();
        queryWrapperForuth.ge("time","2022-10");
        queryWrapperForuth.le("time","2022-12");
        List<Salemonthdata> salemonthdataList4 = salemonthdataMapper.selectList(queryWrapperForuth);

        EchartAnalysis echartAnalysis = new EchartAnalysis();
        //一季度数据
        List<String> firstList = new ArrayList<>();
        firstList.add("第一季度");
        List<String> secondList = new ArrayList<>();
        secondList.add("第二季度");
        List<String> thirdList = new ArrayList<>();
        thirdList.add("第三季度");
        List<String> forthList = new ArrayList<>();
        forthList.add("第四季度");
        List<List> lists = new ArrayList<>();
        List<String> product = new ArrayList<>();
        product.add("product");

        //平均数
        Integer average = 0;

        //封装到表
        Integer i = 1;
        for (Salemonthdata salemonthdata : salemonthdataList1) {
            Integer profit =  salemonthdata.getProjectProfit();
            firstList.add(String.valueOf(profit));
            average +=  profit;
            product.add(i.toString());
            i++;
        }
        for (Salemonthdata salemonthdata : salemonthdataList2) {
            Integer profit =  salemonthdata.getProjectProfit();
            secondList.add(String.valueOf(profit));
            average +=  profit;
        }
        for (Salemonthdata salemonthdata : salemonthdataList3) {
            Integer profit =  salemonthdata.getProjectProfit();
            thirdList.add(String.valueOf(profit));
            average +=  profit;
        }
        for (Salemonthdata salemonthdata : salemonthdataList4) {
            Integer profit =  salemonthdata.getProjectProfit();
            forthList.add(String.valueOf(profit));
            average +=  profit;
        }
        lists.add(product);
        lists.add(firstList);
        lists.add(secondList);
        lists.add(thirdList);
        lists.add(forthList);
        echartAnalysis.setDataset(lists);

        List<EchartAnalysisData> seriesList = new ArrayList<>();
        for (Integer j=1;j<=i;j++){
            EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
            echartAnalysisData.setType("line");
            Map<String,String> map = new HashMap<String,String>();
            map.put("focus","series");
            echartAnalysisData.setEmphasis(map);
            echartAnalysisData.setSeriesLayoutBy("row");
            echartAnalysisData.setSmooth("true");
            seriesList.add(echartAnalysisData);
        }
        EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
        echartAnalysisData.setType("pie");
        echartAnalysisData.setId("pie");
        echartAnalysisData.setRadius("30%");

        List<String> center =  new ArrayList();
        center.add("50%");
        center.add("25%");
        echartAnalysisData.setCenter(center);

        Map<String,String> map = new HashMap<String,String>();
        map.put("focus","self");
        echartAnalysisData.setEmphasis(map);

        Map<String,String> maplabel = new HashMap<String,String>();
        maplabel.put("formatter","{b}: {@2012} ({d}%)");
        echartAnalysisData.setLabel(maplabel);

        Map<String,String> encode = new HashMap<String,String>();
        encode.put("itemName","product");
        encode.put("value","1");
        echartAnalysisData.setEncode(encode);
        seriesList.add(echartAnalysisData);
        echartAnalysis.setSeries(seriesList);
        echartAnalysis.setAverage(average/12);
        return echartAnalysis;
    }

    @Override
    public EchartAnalysis getTemperatureAnalysis() {
        //15<度
        //15-20
        //20-25
        //25-30
        //30+
        QueryWrapper queryWrapperFirst = new QueryWrapper();
        queryWrapperFirst.le("temperature",20);
        List<Saledata> Saledata1 = saledataMapper.selectList(queryWrapperFirst);

        QueryWrapper queryWrapperSecond = new QueryWrapper();
        queryWrapperSecond.ge("temperature",21);
        queryWrapperSecond.le("temperature",25);
        List<Saledata> Saledata2 = saledataMapper.selectList(queryWrapperSecond);

        QueryWrapper queryWrapperThird = new QueryWrapper();
        queryWrapperThird.ge("temperature",26);
        queryWrapperThird.le("temperature",30);
        List<Saledata> Saledata3 = saledataMapper.selectList(queryWrapperThird);

        QueryWrapper queryWrapperForuth = new QueryWrapper();
        queryWrapperForuth.ge("temperature",30);
        List<Saledata> Saledata4 = saledataMapper.selectList(queryWrapperForuth);

        EchartAnalysis echartAnalysis = new EchartAnalysis();
        //一季度数据
        List<String> firstList = new ArrayList<>();
        firstList.add("20度内");
        List<String> secondList = new ArrayList<>();
        secondList.add("20-25度");
        List<String> thirdList = new ArrayList<>();
        thirdList.add("26-30度");
        List<String> forthList = new ArrayList<>();
        forthList.add("30度外");
        List<List> lists = new ArrayList<>();
        List<String> product = new ArrayList<>();
        product.add("product");

        Integer listNum = Saledata1.size()<=Saledata2.size()?Saledata2.size():Saledata1.size();
        listNum = listNum<=Saledata3.size()?Saledata3.size():listNum;
        listNum = listNum<=Saledata4.size()?Saledata4.size():listNum;
        for (Integer a=1;a<=listNum;a++){
            product.add(String.valueOf(a));
        }

        //平均数
        Integer average = 0;
        Integer i = 0;
        //封装到表
        for (Saledata saledata : Saledata1) {
            Integer profit =  saledata.getProjectProfit();
            firstList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledata saledata : Saledata2) {
            Integer profit =  saledata.getProjectProfit();
            secondList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledata saledata : Saledata3) {
            Integer profit =  saledata.getProjectProfit();
            thirdList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledata saledata : Saledata4) {
            Integer profit =  saledata.getProjectProfit();
            forthList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        lists.add(product);
        lists.add(firstList);
        lists.add(secondList);
        lists.add(thirdList);
        lists.add(forthList);
        echartAnalysis.setDataset(lists);

        List<EchartAnalysisData> seriesList = new ArrayList<>();
        for (Integer j=1;j<=4;j++){
            EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
            echartAnalysisData.setType("line");
            Map<String,String> map = new HashMap<String,String>();
            map.put("focus","series");
            echartAnalysisData.setEmphasis(map);
            echartAnalysisData.setSeriesLayoutBy("row");
            echartAnalysisData.setSmooth("true");
            seriesList.add(echartAnalysisData);
        }
        EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
        echartAnalysisData.setType("pie");
        echartAnalysisData.setId("pie");
        echartAnalysisData.setRadius("30%");

        List<String> center =  new ArrayList();
        center.add("50%");
        center.add("25%");
        echartAnalysisData.setCenter(center);

        Map<String,String> map = new HashMap<String,String>();
        map.put("focus","self");
        echartAnalysisData.setEmphasis(map);

        Map<String,String> maplabel = new HashMap<String,String>();
        maplabel.put("formatter","{b}: {@2012} ({d}%)");
        echartAnalysisData.setLabel(maplabel);

        Map<String,String> encode = new HashMap<String,String>();
        encode.put("itemName","product");
        encode.put("value","1");
        echartAnalysisData.setEncode(encode);
        seriesList.add(echartAnalysisData);
        echartAnalysis.setSeries(seriesList);
        echartAnalysis.setAverage(average/i);
        return echartAnalysis;
    }

    @Override
    public EchartAnalysis getWeatherAnalysis() {
        QueryWrapper queryWrapperFirst = new QueryWrapper();
        queryWrapperFirst.eq("weather","雨");
        List<Saledata> Saledata1 = saledataMapper.selectList(queryWrapperFirst);

        QueryWrapper queryWrapperSecond = new QueryWrapper();
        queryWrapperSecond.eq("weather","晴");
        List<Saledata> Saledata2 = saledataMapper.selectList(queryWrapperSecond);

        QueryWrapper queryWrapperThird = new QueryWrapper();
        queryWrapperThird.eq("weather","多云");
        List<Saledata> Saledata3 = saledataMapper.selectList(queryWrapperThird);

        QueryWrapper queryWrapperForuth = new QueryWrapper();
        queryWrapperForuth.eq("weather","阴");
        List<Saledata> Saledata4 = saledataMapper.selectList(queryWrapperForuth);

        EchartAnalysis echartAnalysis = new EchartAnalysis();
        //一季度数据
        List<String> firstList = new ArrayList<>();
        firstList.add("雨");
        List<String> secondList = new ArrayList<>();
        secondList.add("晴");
        List<String> thirdList = new ArrayList<>();
        thirdList.add("多云");
        List<String> forthList = new ArrayList<>();
        forthList.add("阴");
        List<List> lists = new ArrayList<>();
        List<String> product = new ArrayList<>();
        product.add("product");

        Integer listNum = Saledata1.size()<=Saledata2.size()?Saledata2.size():Saledata1.size();
        listNum = listNum<=Saledata3.size()?Saledata3.size():listNum;
        listNum = listNum<=Saledata4.size()?Saledata4.size():listNum;
        for (Integer a=1;a<=listNum;a++){
            product.add(String.valueOf(a));
        }

        //平均数
        Integer average = 0;
        Integer i = 0;
        //封装到表
        for (Saledata saledata : Saledata1) {
            Integer profit =  saledata.getProjectProfit();
            firstList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledata saledata : Saledata2) {
            Integer profit =  saledata.getProjectProfit();
            secondList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledata saledata : Saledata3) {
            Integer profit =  saledata.getProjectProfit();
            thirdList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        for (Saledata saledata : Saledata4) {
            Integer profit =  saledata.getProjectProfit();
            forthList.add(String.valueOf(profit));
            average +=  profit;
            i+=1;
        }
        lists.add(product);
        lists.add(firstList);
        lists.add(secondList);
        lists.add(thirdList);
        lists.add(forthList);
        echartAnalysis.setDataset(lists);

        List<EchartAnalysisData> seriesList = new ArrayList<>();
        for (Integer j=1;j<=4;j++){
            EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
            echartAnalysisData.setType("line");
            Map<String,String> map = new HashMap<String,String>();
            map.put("focus","series");
            echartAnalysisData.setEmphasis(map);
            echartAnalysisData.setSeriesLayoutBy("row");
            echartAnalysisData.setSmooth("true");
            seriesList.add(echartAnalysisData);
        }
        EchartAnalysisData echartAnalysisData = new EchartAnalysisData();
        echartAnalysisData.setType("pie");
        echartAnalysisData.setId("pie");
        echartAnalysisData.setRadius("30%");

        List<String> center =  new ArrayList();
        center.add("50%");
        center.add("25%");
        echartAnalysisData.setCenter(center);

        Map<String,String> map = new HashMap<String,String>();
        map.put("focus","self");
        echartAnalysisData.setEmphasis(map);

        Map<String,String> maplabel = new HashMap<String,String>();
        maplabel.put("formatter","{b}: {@2012} ({d}%)");
        echartAnalysisData.setLabel(maplabel);

        Map<String,String> encode = new HashMap<String,String>();
        encode.put("itemName","product");
        encode.put("value","1");
        echartAnalysisData.setEncode(encode);
        seriesList.add(echartAnalysisData);
        echartAnalysis.setSeries(seriesList);
        echartAnalysis.setAverage(average/i);
        return echartAnalysis;
    }
}
